<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        function arrange(str) {

            const funArr = []

            const fn = () => new Promise((res) => {
                console.log(str + ' start')
                res()
            })
            funArr.push(fn)

            function runTask() {
                if (!funArr.length) return
                const fun = funArr.shift()
                fun().then(runTask)
            }
            return {
                wait: function (second) {
                    const fn = () => new Promise((res) => {
                        setTimeout(() => {
                            console.log('等了' + second + '秒')
                            res()
                        }, second * 1000);
                    })
                    funArr.push(fn)
                    return this
                },
                waitFirst: function (second) {
                    const fn = () => new Promise((res) => {
                        setTimeout(() => {
                            console.log('等了' + second + '秒')
                            res()
                        }, second * 1000);
                    })
                    funArr.unshift(fn)
                    return this
                },
                do: function (str) {
                    const fn = () => new Promise((res) => {
                        console.log(str + ' start')
                    })
                    funArr.push(fn)
                    return this
                },
                execute: function () {
                    runTask()
                    return this
                }
            }
        }

        arrange('guiwu').wait(5).do('commit').waitFirst(3).execute()
    </script>
</body>

</html>